enum num {	NA = 1 "A",		// Define enum for card values
		N2 "2",
		N3 "3",
		N4 "4",
		N5 "5",
		N6 "6",
		N7 "7",
		N8 "8",
		N9 "9",
		N10 "10",
		NJ "J",
		NQ "Q",
		NK "K"};

enum suit {	SPADES = 1 "Spades",	// Define enum for card suits
		HEARTS "Hearts",
		DIAMONDS "Diamonds",
		CLUBS "Clubs"};

enum card <num, suit>;			// Define enum for cards

enum player {	WANG "Minglei",		// Define enum for players
		LU "Chenguang",
		LIN "Yi",
		LI "Bo",
		YANG "Shu"};

cont<enum card> deck;			// Card deck
cont<enum card>[enum player] hand;	// Hand cards for each player
enum player current;			// Current player

int sum(cont<enum card> playerhand)
{
	int sum, i;

	sum = 0;

	for (i = 0; i < size(playerhand); i++) {
		sum = sum + value(extract(peek(playerhand, i), 1));
	}
}

event entry {
	call init;
	call newround;
}

event init {
	enum card temp;
	enum player tempplayer;

	tempplayer = WANG;

	if (size(deck) > 0) {
		do {
			temp = remove(deck, 0);
			insert(hand[tempplayer], top, temp);
			templayer++;
		} while (size(deck) > 0 && tempplayer != WANG);
	}
}

event newround {
	bool hit;

	if(sum(hand[current]) > 21) {
		call gameover;
	}

	output(current, "Hit");
	input(current, hit);

	if (hit) {
		call hit;
	} else {
		call stand;	
	}

	trans newround;
}

event hit {
	enum card temp;

	if (size(deck) > 0) {
		temp = remove(deck, 0);
		insert(hand[current], top, temp);
	}
}

event stand {
	current++;	
}

event gameover {
	enum player tempplayer;
	enum player winner;
	int min;

	templayer = WANG;
	min = 21;
	
	do {
		if (sum(hand[tempplayer]) > 21) {
			output(tempplayer, "$tempplayer win");
		} else {
			if ((21 - sum(hand[tempplayer])) < min) {
				min = (21 - sum(hand[tempplayer]));
				winner = tempplayer;
			}
		}

		tempplayer++;
	} while (tempplayer != WANG);
	
	output(winner, "$winner win");
}

